/**
 * 第三方库统一配置入口
 * 用于管理所有第三方库的引用
 */

// 优先确保process对象存在，在最顶部执行
(function ensureProcess() {
  // 安全获取全局对象
  function getGlobalObject() {
    if (typeof globalThis !== 'undefined') return globalThis;
    if (typeof global !== 'undefined') return global;
    if (typeof window !== 'undefined') return window;
    if (typeof self !== 'undefined') return self;
    return {};
  }
  
  const globalObj = getGlobalObject();
  
  // 确保process对象存在
  if (typeof globalObj.process === 'undefined') {
    globalObj.process = { env: { NODE_ENV: 'production' } };
    try { process = globalObj.process; } catch (e) {}
  }
})();

// 然后再加载polyfill文件
try {
  require('../helpers/mobx_polyfill.js');
} catch (e) {
  console.warn('[libs_config] mobx_polyfill加载失败，使用默认环境设置');
}

// MobX 相关库
let mobx, mobxMiniprogram, mobxMiniprogramBindings;

// 尝试加载补丁版本
try {
  mobx = require('../miniprogram_npm/mobx/patched.js');
} catch (e) {
  // 如果补丁版本加载失败，回退到原始版本
  try {
    mobx = require('../miniprogram_npm/mobx/index.js');
  } catch (e) {
    console.error('[libs_config] MobX加载失败:', e.message);
    // 提供一个空对象作为兼容实现
    mobx = {
      observable: function() { return {}; },
      action: function(fn) { return fn; },
      computed: function() { return {}; },
      configure: function() {},
      runInAction: function(fn) { return fn(); }
    };
  }
}

// 加载mobx-miniprogram库
try {
  mobxMiniprogram = require('../miniprogram_npm/mobx-miniprogram/patched.js');
} catch (e) {
  // 如果补丁版本加载失败，回退到原始版本
  try {
    mobxMiniprogram = require('../miniprogram_npm/mobx-miniprogram/index.js');
  } catch (e) {
    console.error('[libs_config] mobx-miniprogram加载失败:', e.message);
    // 提供一个空对象作为兼容实现
    mobxMiniprogram = {
      observer: function() { return function() {}; },
      observable: mobx.observable,
      action: mobx.action,
      computed: mobx.computed
    };
  }
}

// 加载mobx-miniprogram-bindings库
try {
  mobxMiniprogramBindings = require('../miniprogram_npm/mobx-miniprogram-bindings/patched.js');
} catch (e) {
  // 如果补丁版本加载失败，回退到原始版本
  try {
    mobxMiniprogramBindings = require('../miniprogram_npm/mobx-miniprogram-bindings/index.js');
  } catch (e) {
    console.error('[libs_config] mobx-miniprogram-bindings加载失败:', e.message);
    // 提供一个空对象作为兼容实现
    mobxMiniprogramBindings = {
      createStoreBindings: function() {
        return {
          updateStoreBindings: function() {},
          destroyStoreBindings: function() {}
        };
      },
      storeBindingsBehavior: Behavior({})
    };
  }
}

// Day.js库
let dayjs;
try {
  dayjs = require('../miniprogram_npm/dayjs/index.js');
} catch (e) {
  console.error('[libs_config] dayjs加载失败:', e);
  // 提供一个简单的实现作为兼容
  dayjs = function(date) {
    const d = date ? new Date(date) : new Date();
    return {
      format: function(fmt) {
        return d.toISOString();
      }
    };
  };
}

// 导出所有第三方库
module.exports = {
  mobx: mobx,
  mobxMiniprogram: mobxMiniprogram,
  dayjs: dayjs,
  createStoreBindings: mobxMiniprogramBindings.createStoreBindings,
  storeBindingsBehavior: mobxMiniprogramBindings.storeBindingsBehavior
}; 